home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / os / 4.2bsd / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-14  |  18.7 KB  |  751 lines

  1. /***********************************************************
  2. Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /* $XConsortium: io.c,v 1.65 89/09/14 16:19:50 rws Exp $ */
  25. /*****************************************************************
  26.  * i/o functions
  27.  *
  28.  *   WriteToClient, ReadRequestFromClient
  29.  *   InsertFakeRequest, ResetCurrentRequest
  30.  *
  31.  *****************************************************************/
  32.  
  33. #include <stdio.h>
  34. #include "Xos.h"
  35. #include "Xmd.h"
  36. #include <errno.h>
  37. #include <sys/param.h>
  38. #include <sys/uio.h>
  39. #include "X.h"
  40. #include "Xproto.h"
  41. #include "os.h"
  42. #include "osdep.h"
  43. #include "opaque.h"
  44. #include "dixstruct.h"
  45. #include "misc.h"
  46.  
  47. extern void MarkClientException();
  48. extern long ClientsWithInput[];
  49. extern long ClientsWriteBlocked[];
  50. extern long OutputPending[];
  51. extern long OutputBufferSize;
  52. extern int ConnectionTranslation[];
  53. extern Bool NewOutputPending;
  54. extern Bool AnyClientsWriteBlocked;
  55. static Bool CriticalOutputPending;
  56. static int timesThisConnection = 0;
  57. static ConnectionInputPtr FreeInputs = (ConnectionInputPtr)NULL;
  58. static ConnectionOutputPtr FreeOutputs = (ConnectionOutputPtr)NULL;
  59. static OsCommPtr AvailableInput = (OsCommPtr)NULL;
  60.  
  61. static ConnectionInputPtr AllocateInputBuffer();
  62. static ConnectionOutputPtr AllocateOutputBuffer();
  63.  
  64. extern int errno;
  65.  
  66. #define request_length(req, cli) ((cli->swapped ? \
  67.     lswaps((req)->length) : (req)->length) << 2)
  68. #define MAX_TIMES_PER         10
  69.  
  70. /*****************************************************************
  71.  * ReadRequestFromClient
  72.  *    Returns one request in client->requestBuffer.  Return status is:
  73.  *
  74.  *    > 0  if  successful, specifies length in bytes of the request
  75.  *    = 0  if  entire request is not yet available
  76.  *    < 0  if  client should be terminated
  77.  *
  78.  *    The request returned must be contiguous so that it can be
  79.  *    cast in the dispatcher to the correct request type.  Because requests
  80.  *    are variable length, ReadRequestFromClient() must look at the first 4
  81.  *    bytes of a request to determine the length (the request length is
  82.  *    always the 3rd and 4th bytes of the request).  
  83.  *
  84.  *    Note: in order to make the server scheduler (WaitForSomething())
  85.  *    "fair", the ClientsWithInput mask is used.  This mask tells which
  86.  *    clients have FULL requests left in their buffers.  Clients with
  87.  *    partial requests require a read.  Basically, client buffers
  88.  *    are drained before select() is called again.  But, we can't keep
  89.  *    reading from a client that is sending buckets of data (or has
  90.  *    a partial request) because others clients need to be scheduled.
  91.  *****************************************************************/
  92.  
  93. #define YieldControl()                \
  94.         { isItTimeToYield = TRUE;        \
  95.       timesThisConnection = 0; }
  96. #define YieldControlNoInput()            \
  97.         { YieldControl();            \
  98.       BITCLEAR(ClientsWithInput, fd); }
  99. #define YieldControlDeath()            \
  100.         { timesThisConnection = 0; }
  101.  
  102. int
  103. ReadRequestFromClient(client)
  104.     ClientPtr client;
  105. {
  106.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  107.     register ConnectionInputPtr oci = oc->input;
  108.     int fd = oc->fd;
  109.     int result, gotnow, needed;
  110.     register xReq *request;
  111.  
  112.     if (AvailableInput)
  113.     {
  114.     if (AvailableInput != oc)
  115.     {
  116.         AvailableInput->input->next = FreeInputs;
  117.         FreeInputs = AvailableInput->input;
  118.         AvailableInput->input = (ConnectionInputPtr)NULL;
  119.     }
  120.     AvailableInput = (OsCommPtr)NULL;
  121.     }
  122.     if (!oci)
  123.     {
  124.     if (oci = FreeInputs)
  125.     {
  126.         FreeInputs = oci->next;
  127.     }
  128.     else if (!(oci = AllocateInputBuffer()))
  129.     {
  130.         YieldControlDeath();
  131.         return -1;
  132.     }
  133.     oc->input = oci;
  134.     }
  135.     oci->bufptr += oci->lenLastReq;
  136.  
  137.     request = (xReq *)oci->bufptr;
  138.     gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
  139.     if ((gotnow < sizeof(xReq)) ||
  140.     (gotnow < (needed = request_length(request, client))))
  141.     {
  142.     oci->lenLastReq = 0;
  143.     if ((gotnow < sizeof(xReq)) || (needed == 0))
  144.        needed = sizeof(xReq);
  145.     else if (needed > MAXBUFSIZE)
  146.     {
  147.         YieldControlDeath();
  148.         return -1;
  149.     }
  150.     if ((gotnow == 0) ||
  151.         ((oci->bufptr - oci->buffer + needed) > oci->size))
  152.     {
  153.         if ((gotnow > 0) && (oci->bufptr != oci->buffer))
  154.         bcopy(oci->bufptr, oci->buffer, gotnow);
  155.         if (needed > oci->size)
  156.         {
  157.         char *ibuf;
  158.  
  159.         ibuf = (char *)xrealloc(oci->buffer, needed);
  160.         if (!ibuf)
  161.         {
  162.             YieldControlDeath();
  163.             return -1;
  164.         }
  165.         oci->size = needed;
  166.         oci->buffer = ibuf;
  167.         }
  168.         oci->bufptr = oci->buffer;
  169.         oci->bufcnt = gotnow;
  170.     }
  171.     result = read(fd, oci->buffer + oci->bufcnt, 
  172.               oci->size - oci->bufcnt); 
  173.     if (result <= 0)
  174.     {
  175.         if ((result < 0) && (errno == EWOULDBLOCK))
  176.         {
  177.         YieldControlNoInput();
  178.         return 0;
  179.         }
  180.         YieldControlDeath();
  181.         return -1;
  182.     }
  183.     oci->bufcnt += result;
  184.     gotnow += result;
  185.     /* free up some space after huge requests */
  186.     if ((oci->size > BUFWATERMARK) &&
  187.         (oci->bufcnt < BUFSIZE) && (needed < BUFSIZE))
  188.     {
  189.         char *ibuf;
  190.  
  191.         ibuf = (char *)xrealloc(oci->buffer, BUFSIZE);
  192.         if (ibuf)
  193.         {
  194.         oci->size = BUFSIZE;
  195.         oci->buffer = ibuf;
  196.         oci->bufptr = ibuf + oci->bufcnt - gotnow;
  197.         }
  198.     }
  199.     request = (xReq *)oci->bufptr;
  200.     if ((gotnow < sizeof(xReq)) ||
  201.         (gotnow < (needed = request_length(request, client))))
  202.     {
  203.         YieldControlNoInput();
  204.         return 0;
  205.     }
  206.     }
  207.     if (needed == 0)
  208.     needed = sizeof(xReq);
  209.     oci->lenLastReq = needed;
  210.  
  211.     /*
  212.      *  Check to see if client has at least one whole request in the
  213.      *  buffer.  If there is only a partial request, treat like buffer
  214.      *  is empty so that select() will be called again and other clients
  215.      *  can get into the queue.   
  216.      */
  217.  
  218.     if (gotnow >= needed + sizeof(xReq)) 
  219.     {
  220.     request = (xReq *)(oci->bufptr + needed);
  221.         if (gotnow >= needed + request_length(request, client))
  222.         BITSET(ClientsWithInput, fd);
  223.         else
  224.         YieldControlNoInput();
  225.     }
  226.     else
  227.     {
  228.     if (gotnow == needed)
  229.         AvailableInput = oc;
  230.     YieldControlNoInput();
  231.     }
  232.     if (++timesThisConnection >= MAX_TIMES_PER)
  233.     YieldControl();
  234.  
  235.     client->requestBuffer = (pointer)oci->bufptr;
  236.     return needed;
  237. }
  238.  
  239. /*****************************************************************
  240.  * InsertFakeRequest
  241.  *    Splice a consed up (possibly partial) request in as the next request.
  242.  *
  243.  **********************/
  244.  
  245. Bool
  246. InsertFakeRequest(client, data, count)
  247.     ClientPtr client;
  248.     char *data;
  249.     int count;
  250. {
  251.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  252.     register ConnectionInputPtr oci = oc->input;
  253.     int fd = oc->fd;
  254.     register xReq *request;
  255.     int gotnow, moveup;
  256.  
  257.     if (AvailableInput)
  258.     {
  259.     if (AvailableInput != oc)
  260.     {
  261.         AvailableInput->input->next = FreeInputs;
  262.         FreeInputs = AvailableInput->input;
  263.         AvailableInput->input = (ConnectionInputPtr)NULL;
  264.     }
  265.     AvailableInput = (OsCommPtr)NULL;
  266.     }
  267.     if (!oci)
  268.     {
  269.     if (oci = FreeInputs)
  270.         FreeInputs = oci->next;
  271.     else if (!(oci = AllocateInputBuffer()))
  272.         return FALSE;
  273.     oc->input = oci;
  274.     }
  275.     oci->bufptr += oci->lenLastReq;
  276.     oci->lenLastReq = 0;
  277.     gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
  278.     if ((gotnow + count) > oci->size)
  279.     {
  280.     char *ibuf;
  281.  
  282.     ibuf = (char *)xrealloc(oci->buffer, gotnow + count);
  283.     if (!ibuf)
  284.         return(FALSE);
  285.     oci->size = gotnow + count;
  286.     oci->buffer = ibuf;
  287.     oci->bufptr = ibuf + oci->bufcnt - gotnow;
  288.     }
  289.     moveup = count - (oci->bufptr - oci->buffer);
  290.     if (moveup > 0)
  291.     {
  292.     if (gotnow > 0)
  293.         bcopy(oci->bufptr, oci->bufptr + moveup, gotnow);
  294.     oci->bufptr += moveup;
  295.     oci->bufcnt += moveup;
  296.     }
  297.     bcopy(data, oci->bufptr - count, count);
  298.     oci->bufptr -= count;
  299.     request = (xReq *)oci->bufptr;
  300.     gotnow += count;
  301.     if ((gotnow >= sizeof(xReq)) &&
  302.     (gotnow >= request_length(request, client)))
  303.     BITSET(ClientsWithInput, fd);
  304.     else
  305.     YieldControlNoInput();
  306.     return(TRUE);
  307. }
  308.  
  309. /*****************************************************************
  310.  * ResetRequestFromClient
  311.  *    Reset to reexecute the current request, and yield.
  312.  *
  313.  **********************/
  314.  
  315. ResetCurrentRequest(client)
  316.     ClientPtr client;
  317. {
  318.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  319.     register ConnectionInputPtr oci = oc->input;
  320.     int fd = oc->fd;
  321.     register xReq *request;
  322.     int gotnow;
  323.  
  324.     if (AvailableInput == oc)
  325.     AvailableInput = (OsCommPtr)NULL;
  326.     oci->lenLastReq = 0;
  327.     request = (xReq *)oci->bufptr;
  328.     gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
  329.     if ((gotnow >= sizeof(xReq)) &&
  330.     (gotnow >= request_length(request, client)))
  331.     {
  332.     BITSET(ClientsWithInput, fd);
  333.     YieldControl();
  334.     }
  335.     else
  336.     YieldControlNoInput();
  337. }
  338.  
  339.     /* lookup table for adding padding bytes to data that is read from
  340.         or written to the X socket.  */
  341. static int padlength[4] = {0, 3, 2, 1};
  342.  
  343.  /********************
  344.  * FlushClient()
  345.  *    If the client isn't keeping up with us, then we try to continue
  346.  *    buffering the data and set the apropriate bit in ClientsWritable
  347.  *    (which is used by WaitFor in the select).  If the connection yields
  348.  *    a permanent error, or we can't allocate any more space, we then
  349.  *    close the connection.
  350.  *
  351.  **********************/
  352.  
  353. int
  354. FlushClient(who, oc, extraBuf, extraCount)
  355.     ClientPtr who;
  356.     OsCommPtr oc;
  357.     char *extraBuf;
  358.     int extraCount; /* do not modify... returned below */
  359. {
  360.     register ConnectionOutputPtr oco = oc->output;
  361.     int connection = oc->fd;
  362.     struct iovec iov[3];
  363.     char padBuffer[3];
  364.     long written;
  365.     long padsize;
  366.     long notWritten;
  367.     long todo;
  368.  
  369.     if (!oco)
  370.     return 0;
  371.     written = 0;
  372.     padsize = padlength[extraCount & 3];
  373.     notWritten = oco->count + extraCount + padsize;
  374.     todo = notWritten;
  375.     while (notWritten) {
  376.     long before = written;    /* amount of whole thing written */
  377.     long remain = todo;    /* amount to try this time, <= notWritten */
  378.     int i = 0;
  379.     long len;
  380.  
  381.     /* You could be very general here and have "in" and "out" iovecs
  382.      * and write a loop without using a macro, but what the heck.  This
  383.      * translates to:
  384.      *
  385.      *     how much of this piece is new?
  386.      *     if more new then we are trying this time, clamp
  387.      *     if nothing new
  388.      *         then bump down amount already written, for next piece
  389.      *         else put new stuff in iovec, will need all of next piece
  390.      *
  391.      * Note that todo had better be at least 1 or else we'll end up
  392.      * writing 0 iovecs.
  393.      */
  394. #define InsertIOV(pointer, length) \
  395.     len = (length) - before; \
  396.     if (len > remain) \
  397.         len = remain; \
  398.     if (len <= 0) { \
  399.         before = (-len); \
  400.     } else { \
  401.         iov[i].iov_len = len; \
  402.         iov[i].iov_base = (pointer) + before; \
  403.         i++; \
  404.         remain -= len; \
  405.         before = 0; \
  406.     }
  407.  
  408.     InsertIOV ((char *)oco->buf, oco->count)
  409.     InsertIOV (extraBuf, extraCount)
  410.     InsertIOV (padBuffer, padsize)
  411.  
  412.     errno = 0;
  413.     if ((len = writev(connection, iov, i)) >= 0)
  414.     {
  415.         written += len;
  416.         notWritten -= len;
  417.         todo = notWritten;
  418.     }
  419. #ifdef apollo /* stupid sr10.1 UDS bug - supposedly fixed in sr10.2 */
  420.     else if ((errno == EWOULDBLOCK) && (todo > 4096))
  421.     {
  422.         todo = 4096;
  423.     }
  424. #endif
  425.     else if ((errno == EWOULDBLOCK)
  426. #ifdef SUNSYSV /* check for another brain-damaged OS bug */
  427.          || (errno == 0)
  428. #endif
  429. #ifdef EMSGSIZE /* check for another brain-damaged OS bug */
  430.          || ((errno == EMSGSIZE) && (todo == 1))
  431. #endif
  432.         )
  433.     {
  434.         /* If we've arrived here, then the client is stuffed to the gills
  435.            and not ready to accept more.  Make a note of it and buffer
  436.            the rest. */
  437.         BITSET(ClientsWriteBlocked, connection);
  438.         AnyClientsWriteBlocked = TRUE;
  439.  
  440.         if (written < oco->count)
  441.         {
  442.         if (written > 0)
  443.         {
  444.             oco->count -= written;
  445.             bcopy((char *)oco->buf + written,
  446.               (char *)oco->buf,
  447.               oco->count);
  448.             written = 0;
  449.         }
  450.         }
  451.         else
  452.         {
  453.         written -= oco->count;
  454.         oco->count = 0;
  455.         }
  456.  
  457.         if (notWritten > oco->size)
  458.         {
  459.         unsigned char *obuf;
  460.  
  461.         obuf = (unsigned char *)xrealloc(oco->buf,
  462.                          notWritten +
  463.                          OutputBufferSize);
  464.         if (!obuf)
  465.         {
  466.             close(connection);
  467.             MarkClientException(who);
  468.             oco->count = 0;
  469.             return(-1);
  470.         }
  471.         oco->size = notWritten + OutputBufferSize;
  472.         oco->buf = obuf;
  473.         }
  474.  
  475.         /* If the amount written extended into the padBuffer, then the
  476.            difference "extraCount - written" may be less than 0 */
  477.         if ((len = extraCount - written) > 0)
  478.         bcopy (extraBuf + written,
  479.                (char *)oco->buf + oco->count,
  480.                len);
  481.  
  482.         oco->count = notWritten; /* this will include the pad */
  483.         /* return only the amount explicitly requested */
  484.         return extraCount;
  485.     }
  486. #ifdef EMSGSIZE /* check for another brain-damaged OS bug */
  487.     else if (errno == EMSGSIZE)
  488.     {
  489.         todo >>= 1;
  490.     }
  491. #endif
  492.     else
  493.     {
  494.         close(connection);
  495.         MarkClientException(who);
  496.         oco->count = 0;
  497.         return(-1);
  498.     }
  499.     }
  500.  
  501.     /* everything was flushed out */
  502.     oco->count = 0;
  503.     /* check to see if this client was write blocked */
  504.     if (AnyClientsWriteBlocked)
  505.     {
  506.     BITCLEAR(ClientsWriteBlocked, oc->fd);
  507.      if (! ANYSET(ClientsWriteBlocked))
  508.         AnyClientsWriteBlocked = FALSE;
  509.     }
  510.     if (oco->size > OutputBufferSize)
  511.     {
  512.     unsigned char *obuf;
  513.  
  514.     obuf = (unsigned char *)xrealloc(oco->buf, OutputBufferSize);
  515.     if (obuf)
  516.     {
  517.         oco->size = OutputBufferSize;
  518.         oco->buf = obuf;
  519.     }
  520.     }
  521.     oco->next = FreeOutputs;
  522.     FreeOutputs = oco;
  523.     oc->output = (ConnectionOutputPtr)NULL;
  524.     return extraCount; /* return only the amount explicitly requested */
  525. }
  526.  
  527.  /********************
  528.  * FlushAllOutput()
  529.  *    Flush all clients with output.  However, if some client still
  530.  *    has input in the queue (more requests), then don't flush.  This
  531.  *    will prevent the output queue from being flushed every time around
  532.  *    the round robin queue.  Now, some say that it SHOULD be flushed
  533.  *    every time around, but...
  534.  *
  535.  **********************/
  536.  
  537. void
  538. FlushAllOutput()
  539. {
  540.     register int index, base, mask;
  541.     OsCommPtr oc;
  542.     register ClientPtr client;
  543.  
  544.     if (! NewOutputPending)
  545.     return;
  546.  
  547.     /*
  548.      * It may be that some client still has critical output pending,
  549.      * but he is not yet ready to receive it anyway, so we will
  550.      * simply wait for the select to tell us when he's ready to receive.
  551.      */
  552.     CriticalOutputPending = FALSE;
  553.     NewOutputPending = FALSE;
  554.  
  555.     for (base = 0; base < mskcnt; base++)
  556.     {
  557.     mask = OutputPending[ base ];
  558.     OutputPending[ base ] = 0;
  559.     while (mask)
  560.     {
  561.         index = ffs(mask) - 1;
  562.         mask &= ~lowbit(mask);
  563.         if ((index = ConnectionTranslation[(base << 5) + index]) == 0)
  564.         continue;
  565.         client = clients[index];
  566.         if (client->clientGone)
  567.         continue;
  568.         oc = (OsCommPtr)client->osPrivate;
  569.         if (GETBIT(ClientsWithInput, oc->fd))
  570.         {
  571.         BITSET(OutputPending, oc->fd); /* set the bit again */
  572.         NewOutputPending = TRUE;
  573.         }
  574.         else
  575.         (void)FlushClient(client, oc, (char *)NULL, 0);
  576.     }
  577.     }
  578.  
  579. }
  580.  
  581. void
  582. FlushIfCriticalOutputPending()
  583. {
  584.     if (CriticalOutputPending)
  585.     FlushAllOutput();
  586. }
  587.  
  588. void
  589. SetCriticalOutputPending()
  590. {
  591.     CriticalOutputPending = TRUE;
  592. }
  593.  
  594. /*****************
  595.  * WriteToClient
  596.  *    Copies buf into ClientPtr.buf if it fits (with padding), else
  597.  *    flushes ClientPtr.buf and buf to client.  As of this writing,
  598.  *    every use of WriteToClient is cast to void, and the result
  599.  *    is ignored.  Potentially, this could be used by requests
  600.  *    that are sending several chunks of data and want to break
  601.  *    out of a loop on error.  Thus, we will leave the type of
  602.  *    this routine as int.
  603.  *****************/
  604.  
  605. int
  606. WriteToClient (who, count, buf)
  607.     ClientPtr who;
  608.     char *buf;
  609.     int count;
  610. {
  611.     OsCommPtr oc = (OsCommPtr)who->osPrivate;
  612.     register ConnectionOutputPtr oco = oc->output;
  613.     int padBytes;
  614.  
  615.     if (!count)
  616.     return(0);
  617.  
  618.     if (!oco)
  619.     {
  620.     if (oco = FreeOutputs)
  621.     {
  622.         FreeOutputs = oco->next;
  623.     }
  624.     else if (!(oco = AllocateOutputBuffer()))
  625.     {
  626.         close(oc->fd);
  627.         MarkClientException(who);
  628.         return -1;
  629.     }
  630.     oc->output = oco;
  631.     }
  632.  
  633.     padBytes =  padlength[count & 3];
  634.  
  635.     if (oco->count + count + padBytes > oco->size)
  636.     {
  637.     BITCLEAR(OutputPending, oc->fd);
  638.     CriticalOutputPending = FALSE;
  639.     NewOutputPending = FALSE;
  640.     return FlushClient(who, oc, buf, count);
  641.     }
  642.  
  643.     NewOutputPending = TRUE;
  644.     BITSET(OutputPending, oc->fd);
  645.     bcopy(buf, (char *)oco->buf + oco->count, count);
  646.     oco->count += count + padBytes;
  647.     
  648.     return(count);
  649. }
  650.  
  651. static ConnectionInputPtr
  652. AllocateInputBuffer()
  653. {
  654.     register ConnectionInputPtr oci;
  655.  
  656.     oci = (ConnectionInputPtr)xalloc(sizeof(ConnectionInput));
  657.     if (!oci)
  658.     return (ConnectionInputPtr)NULL;
  659.     oci->buffer = (char *)xalloc(BUFSIZE);
  660.     if (!oci->buffer)
  661.     {
  662.     xfree(oci);
  663.     return (ConnectionInputPtr)NULL;
  664.     }
  665.     oci->size = BUFSIZE;
  666.     oci->bufptr = oci->buffer;
  667.     oci->bufcnt = 0;
  668.     oci->lenLastReq = 0;
  669.     return oci;
  670. }
  671.  
  672. static ConnectionOutputPtr
  673. AllocateOutputBuffer()
  674. {
  675.     register ConnectionOutputPtr oco;
  676.  
  677.     oco = (ConnectionOutputPtr)xalloc(sizeof(ConnectionOutput));
  678.     if (!oco)
  679.     return (ConnectionOutputPtr)NULL;
  680.     oco->buf = (unsigned char *) xalloc(OutputBufferSize);
  681.     if (!oco->buf)
  682.     {
  683.     xfree(oco);
  684.     return (ConnectionOutputPtr)NULL;
  685.     }
  686.     oco->size = OutputBufferSize;
  687.     oco->count = 0;
  688.     return oco;
  689. }
  690.  
  691. void
  692. FreeOsBuffers(oc)
  693.     OsCommPtr oc;
  694. {
  695.     register ConnectionInputPtr oci;
  696.     register ConnectionOutputPtr oco;
  697.  
  698.     if (AvailableInput == oc)
  699.     AvailableInput = (OsCommPtr)NULL;
  700.     if (oci = oc->input)
  701.     {
  702.     if (FreeInputs)
  703.     {
  704.         xfree(oci->buffer);
  705.         xfree(oci);
  706.     }
  707.     else
  708.     {
  709.         FreeInputs = oci;
  710.         oci->next = (ConnectionInputPtr)NULL;
  711.         oci->bufptr = oci->buffer;
  712.         oci->bufcnt = 0;
  713.         oci->lenLastReq = 0;
  714.     }
  715.     }
  716.     if (oco = oc->output)
  717.     {
  718.     if (FreeOutputs)
  719.     {
  720.         xfree(oco->buf);
  721.         xfree(oco);
  722.     }
  723.     else
  724.     {
  725.         FreeOutputs = oco;
  726.         oco->next = (ConnectionOutputPtr)NULL;
  727.         oco->count = 0;
  728.     }
  729.     }
  730. }
  731.  
  732. void
  733. ResetOsBuffers()
  734. {
  735.     register ConnectionInputPtr oci;
  736.     register ConnectionOutputPtr oco;
  737.  
  738.     while (oci = FreeInputs)
  739.     {
  740.     FreeInputs = oci->next;
  741.     xfree(oci->buffer);
  742.     xfree(oci);
  743.     }
  744.     while (oco = FreeOutputs)
  745.     {
  746.     FreeOutputs = oco->next;
  747.     xfree(oco->buf);
  748.     xfree(oco);
  749.     }
  750. }
  751.